//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "TIntegForm.h"
#include "TMainForm.h"
#include "TGraphForm.h"
#include "Edv.h"
#include "TRichEditForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CSPIN"
#pragma resource "*.dfm"
TIntegForm *IntegForm;
const TOP = 5;
const LEFT = 10;
const TABV = 5;
const TABH = 5;
//---------------------------------------------------------------------------
__fastcall TIntegForm::TIntegForm(TComponent* Owner)
        :
    TForm(Owner)
{
   Area = new TArea(NULL);
   GRA = new TGRA(NULL, Area);
}
//---------------------------------------------------------------------------

void __fastcall TIntegForm::SetCurTopic(AnsiString TopicName)
{
  if (FileExists(MainForm->Path + INTEG_DIR + AnsiReplaceStr(TopicName, " ", "_") + ".rtf"))
    DescBtn->Visible = true;
  else DescBtn->Visible = false;
  Memo->Lines->LoadFromFile(MainForm->Path + INTEG_DIR + TopicName + ".grt");
  int i = 0;
  i++;
  // topic name
  NameMemo->Text = Memo->Lines->Strings[i]; i++;
  i++;
  NumParams = StrToInt(Memo->Lines->Strings[i]); i++;
  int Top = TOP;
  AnsiString Str;
  // parameters fields
  for(int j = 0; j < NumParams; j++) {
    Label[j] = new TLabel(Frame2);
    Label[j]->Parent = Frame2;
    Label[j]->Top = Top;
    Label[j]->Left = LEFT;
    Label[j]->Width = 130;
    Label[j]->Alignment = taRightJustify;

    Edit[j] = new TEdit(Frame2);
    Edit[j]->Parent = Frame2;
    Edit[j]->Top = Top;
    Edit[j]->Left = LEFT + Label[j]->Width + TABH;
    Edit[j]->Width = 100;

    UnitsG[j] = new TUnitsGroup();

    ComboB[j] = new TComboBox(Frame2);
    ComboB[j]->Parent = Frame2;
    ComboB[j]->Top = Top;
    ComboB[j]->Left = LEFT + Label[j]->Width + Edit[j]->Width + 2*TABH;
    ComboB[j]->Width = 120;

    Str = Memo->Lines->Strings[i]; // parameter string
    PS[j] = "<" + GetBrParam(Str, 1) + ">";
    Label[j]->Caption = GetBrParam(Str, 2);
    UnitsG[j]->SetSort(TGroupSort(StrToInt(GetBrParam(Str, 3))));
    UnitsG[j]->Active = StrToInt(GetBrParam(Str, 4));
    Edit[j]->Text = GetBrParam(Str, 5);
    if (UnitsG[j]->Sort == 0)
      ComboB[j]->Visible = false;
    ComboB[j]->Clear();
    for ( int k = 0; k < UnitsG[j]->Count; k++ )
      ComboB[j]->Items->Add(UnitsG[j]->Units[k].Caption);
    ComboB[j]->ItemIndex = UnitsG[j]->Active;
    Top += Edit[0]->Height + TABV;
    i++;
  }
  i++;

  Formula = Memo->Lines->Strings[i]; i++;
  i++;
  DesMemo->Text = Memo->Lines->Strings[i]; i++;

  Area->PictName = MainForm->Path + INTEG_DIR + TopicName + ".bmp";
  Area->FromFile(i, Memo);
  GRA->FromFile(i, Memo);
}
//---------------------------------------------------------------------------
void __fastcall TIntegForm::FormClose(TObject *Sender,
      TCloseAction &Action)
{
  for(int j = 0; j < NumParams; j++) {
    delete Label[j];
    delete Edit[j];
    delete ComboB[j];
  }
  MainForm->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIntegForm::ExecuteBtnClick(TObject *Sender)
{
  int i = MainForm->nr;
  Application->CreateForm(__classid(TGraphForm), &GraphForm[i]);
  GraphForm[i]->Caption = NameMemo->Text;
  GraphForm[i]->Area->Assign(Area);
  GraphForm[i]->GRA[0] = new TGRA(GraphForm[i]->Canvas, GraphForm[i]->Area);
  GraphForm[i]->GRA[0]->AssignV(GRA, true, true);
  GraphForm[i]->GRA[0]->Make(GenFunction());
  GraphForm[i]->GRA[0]->Canvas = GraphForm[i]->Canvas;
  GraphForm[i]->GRA[0]->Area = GraphForm[i]->Area;
  MainForm->GraphsCLB->AddItem(GRA->PubStr, NULL);
  MainForm->GraphsCLB->Checked[0] = true;
  GraphForm[i]->NumGraphs++;
  GraphForm[i]->Area->Centre();
  GraphForm[i]->Repaint();
  Close();
}
//---------------------------------------------------------------------------
AnsiString __fastcall TIntegForm::GenFunction()
{
  AnsiString Str = Formula;
  AnsiString Str2;
  for(int j = 0; j < NumParams; j++) {
    Str2 = FloatToStr(UnitsG[j]->RealParam(Edit[j]->Text.ToDouble()));
    Str = StringReplace(Str, PS[j], Str2, TReplaceFlags() << rfReplaceAll);
  }
  Str = Str;
  return Str;
}
//---------------------------------------------------------------------------
void __fastcall TIntegForm::DescBtnClick(TObject *Sender)
{
  RichEditForm->Caption = NameMemo->Text;
  RichEditForm->RE->Lines->LoadFromFile(MainForm->Path + "Integration/" + AnsiReplaceStr(NameMemo->Text, " ", "_") + ".rtf");
  RichEditForm->Show();
}
//---------------------------------------------------------------------------
void __fastcall TIntegForm::CancelBtnClick(TObject *Sender)
{
  Close();        
}
//---------------------------------------------------------------------------

